<template>
  <div class="login-bg">
    <div class="login-container">
      <div class="reset-title">重置密码</div>
      <p class="reset-text">输入你的邮箱，我们将发送重置密码邮件</p>
      <el-form :model="param" :rules="rules" ref="register" size="large">
        <el-form-item prop="oldPwd">
          <el-input type="password" v-model="param.oldPwd" placeholder="旧密码">
            <template #prepend>
              <el-icon>
                <Lock/>
              </el-icon>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item prop="newPwd">
          <el-input type="password" v-model="param.newPwd" placeholder="新密码">
            <template #prepend>
              <el-icon>
                <Lock/>
              </el-icon>
            </template>
          </el-input>
        </el-form-item>
        <el-button class="login-btn" type="primary" size="large" @click="submitForm(register)"
        >修改密码
        </el-button
        >
        <p class="login-text">
          <el-link type="primary" @click="$router.push('/login')">返回登录</el-link>
        </p>
      </el-form>
    </div>
  </div>
</template>

<script setup lang="ts">
import {getCurrentInstance, ref} from 'vue';
import {ElMessage, type FormInstance, type FormRules} from 'element-plus';
import {Lock} from "@element-plus/icons-vue";
import {useRouter} from "vue-router";
import CryptoJS from "crypto-js";

const api = getCurrentInstance()?.appContext.config.globalProperties.$api;
const router = useRouter();
const param = ref({
  oldPwd: '',
  newPwd: '',
});

const rules: FormRules = {
  oldPwd: [
    {required: true, message: '请输入旧密码', trigger: 'blur'},
    {
      trigger: 'blur',
    },
  ],
  newPwd: [
    {required: true, message: '请输入新密码', trigger: 'blur'},
    {

      trigger: 'blur',
    },
  ],
};
const register = ref<FormInstance>();
const submitForm = (formEl: FormInstance | undefined) => {
  if (!formEl) return;
  formEl.validate(async (valid: boolean) => {
    if (valid) {
      let userName = localStorage.getItem("userName")
      const encryptedValue = CryptoJS.MD5(param.value.oldPwd).toString();
      let params = {
        "password": encryptedValue,
        "newPassword": param.value.newPwd,
        "userName": userName,
      }
      let res = await api.post("/api/user/resetPassword", params)
      if (res.code == -1) {
        ElMessage.error(res.msg)
      } else if (res.code == 0) {
        ElMessage.success(res.msg)
        localStorage.clear()
        router.push('/login');
      } else {
        ElMessage.error('未知错误')
      }
    } else {
      return false;
    }
  });
};
</script>

<style scoped>
.login-bg {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 100%;
  height: 100vh;
  background: url(../../assets/img/login-bg.jpg) center/cover no-repeat;
}

.reset-title {
  text-align: center;
  font-size: 22px;
  color: #333;
  font-weight: bold;
  margin-bottom: 10px;
}

.reset-text {
  text-align: center;
  font-size: 14px;
  color: #787878;
  margin-bottom: 40px;
}

.login-container {
  width: 450px;
  border-radius: 5px;
  background: #fff;
  padding: 40px 50px 50px;
  box-sizing: border-box;
}

.login-btn {
  display: block;
  width: 100%;
}

.login-text {
  display: flex;
  align-items: center;
  justify-content: center;
  margin-top: 20px;
  font-size: 14px;
  color: #333;
}
</style>
